<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/2/25
 * Time: 18:18
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Orderz extends Base
{
    /**
     * 赠品订单首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        $date = date('Y-m-01 00:00:01');
        Db::table('up_bo_orderz')
            ->where([
                'boz_create_time' => ['<', $date],
                'boz_is_pay' => 1,
                'boz_status' => 1
            ])
            ->update(['boz_status' => 6, 'boz_close_time' => date('Y-m-d H:i:s')]);
        if(request()->isAjax ()){
            $keyword = trim(input('keyword'), ' '); // 订单号或ID
            $bog_status = input('boz_status');
            $is_pay = input('is_pay');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $keyword_user = input('keyword_user');
            $boz_pay_type = input('boz_pay_type');
            $boz_dui = input('boz_dui');
        
            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['boz_id|boz_number'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($keyword_user) && $keyword_user != ''){
                $where['user_real_name|user_phone'] = ['like', '%' . $keyword_user . '%'];
            }
            if(isset($bog_status) && $bog_status != ''){
                if($bog_status != -1){
                    $where['boz_status'] = $bog_status;
                }
            }else{
                $where['boz_status'] = 2;
            }
            if(isset($is_pay) && $is_pay != ''){
                $where['boz_is_pay'] = $is_pay;
            }
            if(isset($boz_pay_type) && $boz_pay_type != ''){
                $where['boz_pay_type'] = $boz_pay_type;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['boz_create_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['boz_create_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['boz_create_time'] = ['<=', $end_time];
            }
            if($boz_dui && $boz_dui != ''){
                $where['boz_dui'] = $boz_dui;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
        
            $count = Db::table('up_bo_orderz')
                ->join('up_u_user', 'boz_user_id = user_id', 'LEFT')
                ->join('up_user_address', 'addr_id = boz_addr_id', 'LEFT')
                ->join('up_area u1', 'u1.district_id = addr_sheng', 'LEFT')
                ->join('up_area u2', 'u2.district_id = addr_shi', 'LEFT')
                ->join('up_area u3', 'u3.district_id = addr_qu', 'LEFT')
                ->join('up_bo_giftbag', 'gift_id = boz_gift_id', 'LEFT')
                ->where($where)
                ->count('boz_id');
            $lists = Db::table('up_bo_orderz')
                ->join('up_u_user', 'boz_user_id = user_id', 'LEFT')
                ->join('up_user_address', 'addr_id = boz_addr_id', 'LEFT')
                ->join('up_area u1', 'u1.district_id = addr_sheng', 'LEFT')
                ->join('up_area u2', 'u2.district_id = addr_shi', 'LEFT')
                ->join('up_area u3', 'u3.district_id = addr_qu', 'LEFT')
                ->join('up_bo_giftbag', 'gift_id = boz_gift_id', 'LEFT')
                ->field('boz_id, boz_number, boz_is_pay, boz_status, boz_remark, boz_status, boz_price, 
                boz_create_time, boz_express_company, boz_express_code, user_real_name, user_phone, 
                concat(u1.district,"",u2.district, "", u3.district, "", addr_address) address, 
                addr_user_name, addr_user_phone, user_shop_name, gift_name, boz_get_month, boz_no, boz_pay_type, 
                boz_eleven, boz_freight, boz_dui, boz_order_id, boz_dui_admin')
                ->where($where)
                ->order('boz_create_time desc')
                ->page($Nowpage, $limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 发货
     * @return mixed|\think\response\Json
     */
    public function orderSend(){
        $boz_id = input('boz_id');
        if(Request::instance()->isPost()){
            $boz_express_company = trim(input('boz_express_company'));
            $boz_express_code = trim(input('boz_express_code'));
            
            $order_status = Db::table('up_bo_orderz')->where('boz_id', $boz_id)->value('boz_status');
            if($order_status != 2) return json(['code' => 100, 'msg' => '订单状态异常，请刷新列表重试']);
            
            $res = Db::table('up_bo_orderz')
                ->update([
                    'boz_id' => $boz_id,
                    'boz_express_company' => $boz_express_company,
                    'boz_express_code' => $boz_express_code,
                    'boz_send_time' => date('Y-m-d H:i:s'),
                    'boz_status' => 3
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $wuliu = Db::table('up_about')->where('id', 7)->value('content');
        return $this->fetch('', ['boz_id' => $boz_id, 'wuliu' => $wuliu]);
    }
    
    /**
     * 编辑物流
     * @return mixed|\think\response\Json
     */
    public function updateWuLiu(){
        $boz_id = input('boz_id');
        if(Request::instance()->isPost()){
            $boz_express_company = trim(input('boz_express_company'));
            $boz_express_code = trim(input('boz_express_code'));
            
            $res = Db::table('up_bo_orderz')
                ->update([
                    'boz_id' => $boz_id,
                    'boz_express_company' => $boz_express_company,
                    'boz_express_code' => $boz_express_code
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $wuliu = Db::table('up_bo_orderz')->where('boz_id', $boz_id)->field('boz_express_company, boz_express_code')->find();
        return $this->fetch('', ['wuliu' => $wuliu, 'boz_id' => $boz_id]);
    }

    /**
     * 已对账
     * @return \think\response\Json
     */
    public function orderDui(){
        $boz_id = input('id');
        $res = Db::table('up_bo_orderz')
            ->where('boz_id', $boz_id)
            ->update(['boz_dui' => 2, 'boz_dui_admin' => session('username')]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $order_ids = input('ids');

        $where = [];
        if($order_ids == -1){
            $keyword = trim(input('keyword'), ' '); // 订单号或ID
            $bog_status = input('boz_status');
            $is_pay = input('is_pay');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $keyword_user = input('keyword_user');
            $boz_pay_type = input('boz_pay_type');
            $boz_dui = input('boz_dui');

            if(isset($keyword) && $keyword != ''){
                $where['boz_id|boz_number'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($keyword_user) && $keyword_user != ''){
                $where['user_real_name|user_phone'] = ['like', '%' . $keyword_user . '%'];
            }
            if(isset($bog_status) && $bog_status != ''){
                if($bog_status != -1){
                    $where['boz_status'] = $bog_status;
                }
            }else{
                $where['boz_status'] = 2;
            }
            if(isset($is_pay) && $is_pay != ''){
                $where['boz_is_pay'] = $is_pay;
            }
            if(isset($boz_pay_type) && $boz_pay_type != ''){
                $where['boz_pay_type'] = $boz_pay_type;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['boz_create_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['boz_create_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['boz_create_time'] = ['<=', $end_time];
            }
            if($boz_dui && $boz_dui != ''){
                $where['boz_dui'] = $boz_dui;
            }
        }else{
            if($order_ids == ''){
                return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            }
            $where['boz_id'] = ['IN', $order_ids];
        }

        $lists = Db::table('up_bo_orderz')
            ->join('up_u_user', 'boz_user_id = user_id', 'LEFT')
            ->join('up_user_address', 'addr_id = boz_addr_id', 'LEFT')
            ->join('up_area u1', 'u1.district_id = addr_sheng', 'LEFT')
            ->join('up_area u2', 'u2.district_id = addr_shi', 'LEFT')
            ->join('up_area u3', 'u3.district_id = addr_qu', 'LEFT')
            ->join('up_bo_giftbag', 'boz_gift_id = gift_id', 'LEFT')
            ->field('boz_number, boz_is_pay, boz_price, boz_remark, boz_status, boz_create_time, 
            boz_express_company, boz_express_code, user_real_name, user_phone, boz_pay_time, 
            boz_send_time, boz_over_time, addr_user_name, addr_user_phone, 
            concat(u1.district,"",u2.district, "", u3.district, "", addr_address) address, 
            user_shop_name, gift_name, boz_get_month, boz_id')
            ->where($where)
            ->order('boz_create_time desc')
            ->select();
        foreach($lists as $k => $v){
            // 物流单号
            $lists[$k]['boz_express_code'] = "\t". $v['boz_express_code'] ."\t";
            // 下单人手机号
            $lists[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
            // 订单号
//            $lists[$k]['bo_number'] = "\t" . $v['order_number'] . "\t";
            // 运费
//            $lists[$k]['bog_total_price'] = "\t" . $v['bog_total_price'] . "\t";
            // 支付状态
            $lists[$k]['boz_is_pay'] = $v['boz_is_pay'] == 1 ? '未支付' : '已支付';
            // 订单状态
            switch($v['boz_status']){
                case 1:
                    $lists[$k]['boz_status'] = '待付款';
                    break;
                case 2:
                    $lists[$k]['boz_status'] = '待发货';
                    break;
                case 3:
                    $lists[$k]['boz_status'] = '待收货';
                    break;
                case 4:
                    $lists[$k]['boz_status'] = '已完成';
                    break;
                case 5:
                    $lists[$k]['boz_status'] = '已取消';
                    break;
                case 6:
                    $lists[$k]['boz_status'] = '已关闭';
                    break;
                default:
                    $lists[$k]['bog_status'] = '未知';
            }
        
        }
        $cellname = [
            ['boz_id', '订单ID', 10, 'LEFT'],
            ['boz_number', '订单编号', 20, 'LEFT'],
            ['gift_name', '礼包', 30, 'LEFT'],
            ['boz_is_pay', '支付状态', 15, 'LEFT'],
            ['boz_price', '总价', 15, 'LEFT'],
            ['boz_status', '订单状态', 15, 'LEFT'],
            ['user_real_name', '下单用户', 15, 'LEFT'],
            ['user_phone', '下单手机号', 15, 'LEFT'],
            ['boz_remark', '备注', 20, 'LEFT'],
            ['address', '收获地址', 30, 'LEFT'],
            ['addr_user_name', '收货人', 15, 'LEFT'],
            ['addr_user_phone', '收货人手机号', 15, 'LEFT'],
            ['boz_get_month', '领取月份', 15, 'LEFT'],
            ['user_shop_name', '店铺名称', 15, 'LEFT'],
            ['boz_express_company', '快递公司', 15, 'LEFT'],
            ['boz_express_code', '快递单号', 20, 'LEFT'],
            ['boz_create_time', '下单时间', 20, 'LEFT'],
            ['boz_pay_time', '付款时间', 20, 'LEFT'],
            ['boz_send_time', '发货时间', 20, 'LEFT'],
            ['boz_over_time', '收货时间', 20, 'LEFT']
        ];
        $res = exportExcelZhy('赠品订单', 'Zeng_order', $cellname, $lists);
        return json($res);
    }
}